function dataout = tca_analysis(datain, fflag)
    switch fflag
        case 'compute_res_curve'
            data = datain{1};
            nrp = 10;
            if length(datain) < 2
                rg = 1: 100;
            end
            res = zeros(nrp, length(rg));
            for ii = 1: nrp
                parfor i = rg
                    [tmp, ~, rect] = ncp(tensor(data), rg(i));
                    res(ii, i) = rect.final.rel_Error;
%                     rec = reconst_tensor(tmp);
%                     ttmp = data - rec;
%                     res(i) = sum(ttmp(:) .^ 2) / sum(data(:) .^ 2);
                end
            end
            
            dataout = {res};
            
        case 'compute_reconst'
            data = datain{1};
            rec = reconst_tensor(data);
            dataout = {rec};
    end
end

%% aux functions %%
function rec = reconst_tensor(data)
    rec = zeros(size(data.U{1}, 1), size(data.U{2}, 1), size(data.U{3}, 1));
    for j = 1: size(data.U{3}, 1)
        ttmp = data.U{1} * diag(data.U{3}(j, :)) * data.U{2}';
        rec(:, :, j) = ttmp;
    end
end